package com.fau.hm.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

/**
 * @desc 组织机构分组表
 * @author dangjh
 * @datetime 2014年10月12日,下午4:25:22
 */
@Entity
@Table(name = "medical_group")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MedicalGroup implements Serializable, JSONAware {
	
	private static final long serialVersionUID = -5244047494917630969L;

	private String id;
	
	@NotBlank(message = "组名称不能为空！")
	@Length(max = 100, message = "组名称长度需要在0和100之间")
	private String name;
	private String description;
	
	@NotBlank(message = "所属医院id不能为空！")
	private String eid;
	
	private MedicalPlatform platform;
	private Date createTime = new Date();
	private Date updateTime = new Date();
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "name", length = 100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	@Column(name = "eid", length = 32)
	public String getEid() {
		return eid;
	}

	public void setEid(String eid) {
		this.eid = eid;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "platform_id")
	public MedicalPlatform getPlatform() {
		return platform;
	}

	public void setPlatform(MedicalPlatform platform) {
		this.platform = platform;
	}
	
	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("name", name);
		obj.put("description", description);
		if(null != platform){
			obj.put("platformId", platform.getId());
			obj.put("platformName", platform.getName());
		}
		obj.put("eid", eid);
		obj.put("createTime", DateFormatUtils.format(createTime, "yyyy-MM-dd"));
		obj.put("updateTime", DateFormatUtils.format(updateTime, "yyyy-MM-dd"));
		return obj.toJSONString();
	}

}
